package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	mapDeserialization()
	structDeserialization()
	sliceDeserialization()
}

type Student struct {
	Name  string  `json:"name"`
	Age   int     `json:"age"`
	Score float64 `json:"score"`
}

func mapDeserialization() {
	var mapStr = `{"age":18,"name":"tom"}`
	var mapStudent map[string]interface{}
	err := json.Unmarshal([]byte(mapStr), &mapStudent)
	if err != nil {
		fmt.Println("mapStr 反序列化失败：", err)
		return
	}
	fmt.Println("mapStr 反序列化后：", mapStudent)
}

func structDeserialization() {
	var structStr = `{"age":18,"name":"tom"}`
	var studentTom Student
	err := json.Unmarshal([]byte(structStr), &studentTom)
	if err != nil {
		fmt.Println("structStr 反序列化失败：", err)
		return
	}
	fmt.Println("structStr 反序列化后：", studentTom)
}

func sliceDeserialization() {
	var sliceStr = `[{"name":"Tom","age":18,"score":65.6},{"name":"Jay","age":19,"score":75.6}] `
	var studentSlice []Student
	err := json.Unmarshal([]byte(sliceStr), &studentSlice)
	if err != nil {
		fmt.Println("sliceStr 反序列化失败：", err)
		return
	}
	fmt.Println("sliceStr 反序列化后：", studentSlice)
}
